package com.example.administrator.test.fragment;


import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.example.administrator.test.ChangePassengerActivity;
import com.example.administrator.test.PoiSearchActivity;
import com.example.administrator.test.R;
import com.example.administrator.test.login.RecordSQLiteOpenHelper;
import com.example.administrator.test.login.CodeActivity;
import com.example.administrator.test.order.WaiteActivity;

import utils.FixPosition;
import utils.MySkipPoiSearch;
import utils.SendOrder;
import utils.ShowNearCar;
import utils.SubmitOrder;

/**
 * Created by Administrator on 2016/11/18.
 */
public class FragmentSpecial extends Fragment {
    private BaiduMap baiduMap;
    private TextureMapView mapView;
    private GeoCoder geoCoder;//地理编码检索类
    private TextView tv_fragment_special_start,tv_fragment_special_end;//行程开始、结束定位地点
    private String sStartLongitude,sStartLatitude, sEndLongitude, sEndLatitude;//开始、结束地点经纬度  String类型
    private double dStartLatitude,dStartLongitude, dEndLongitude, dEndLatitude;//开始、结束地点经纬度  double类型
    private static LinearLayout ll_special_bottom;//隐藏布局
    private int j=0;//标志位  用于判断是专车还是货运(0:专车 1：货运)
    private int carType=0;//标志位  用于判断是专车还是货运(0:专车 1：货运)  传递车辆类型数据给后台
    private String locPlace,endAdress;//开始、结束地点的名称
    private RadioGroup rg_special;//单选按钮
    private RadioButton rb_special;
    private  int rb_id;//被选中RadioButton id
    private TextView tv_money_special,tv_special_name;
    private String newToken;
    private Button btn_callSpecialCar;
    private TextView tv_special_change;
    private String name;
    private Intent intent;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        SDKInitializer.initialize(getActivity());
        View view = inflater.inflate(R.layout.fragment_special, null);
        tv_special_name= (TextView) view.findViewById(R.id.tv_special_name);
        intent=getActivity().getIntent();
        name=intent.getStringExtra("name");
        tv_special_name.setText(name);
        tv_special_change= (TextView) view.findViewById(R.id.tv_special_change);
        tv_special_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), ChangePassengerActivity.class);
                startActivity(intent);
            }
        });
        newToken= CodeActivity.getToken();
        //获取开始、结束地点经纬度以及开始地点名称的方法
        getSpecialSendOrderInformation();
        rg_special= (RadioGroup) view.findViewById(R.id.rg_special);
        //当用户选择结束地点后才调用该方法
        if(!sEndLongitude.equals("0.0")&&!sEndLatitude.equals("0.0")){
            rb_id=clickSpecialCarType(rg_special,view);
        }
        btn_callSpecialCar= (Button) view.findViewById(R.id.btn_callSpecialCar);
        btn_callSpecialCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(rb_id==R.id.rb1_special) {
                    SubmitOrder.submitOrder(newToken, locPlace, sStartLongitude, sStartLatitude,
                            endAdress, sEndLongitude, sEndLatitude, "0");
                }else if(rb_id==R.id.rb2_special){
                    SubmitOrder.submitOrder(newToken, locPlace, sStartLongitude,
                            sStartLatitude, endAdress, sEndLongitude, sEndLatitude, "1");
                }else if(rb_id==R.id.rb3_special){
                    SubmitOrder.submitOrder(newToken, locPlace, sStartLongitude,
                            sStartLatitude, endAdress, sEndLongitude, sEndLatitude, "2");
                }
                Intent intent=new Intent(getContext(), WaiteActivity.class);
                startActivity(intent);
            }
        });
        tv_money_special= (TextView) view.findViewById(R.id.tv_money_special);
        ll_special_bottom= (LinearLayout) view.findViewById(R.id.ll_special_bottom);
//        ll_special_bottom.setVisibility(View.GONE);//设置布局隐藏
        geoCoder = GeoCoder.newInstance();//创建地理编码检索实例
        tv_fragment_special_start= (TextView) view.findViewById(R.id.tv_fragment_special_start);
        tv_fragment_special_end= (TextView) view.findViewById(R.id.tv_fragment_special_end);
        //调用点击结束地点跳转到poiSearchActivity的方法
        MySkipPoiSearch.MySkipPoiSearch(getActivity(),tv_fragment_special_end);
        intent=getActivity().getIntent();
        String endAddress=intent.getStringExtra("endAdress");
        tv_fragment_special_end.setText(endAddress);
        //获取结束地点名称
        endAdress=intent.getStringExtra("endAdress");
        int tag=intent.getIntExtra("tag",0);
        if(tag==1){//如果标志位为1，则展开隐藏布局
            ll_special_bottom.setVisibility(View.VISIBLE);
        }
        tv_fragment_special_end.setText(endAdress);
        mapView = (TextureMapView) view.findViewById(R.id.special_mapview);
        baiduMap = mapView.getMap();
        //定位方法
//        FixPosition.FixPosition(mapView,baiduMap,getContext(),geoCoder,tv_fragment_special_start);
        mapView.showZoomControls(false);//不显示缩放控件
        String carType1=carType+"";//车辆类型 int->String
        //调用显示附近车辆的方法  当经纬度都不为0的时候
        if(!sStartLatitude.equals("0.0")&&!sStartLongitude.equals("0.0")){
            ShowNearCar.ShowNearCar(baiduMap,carType1,sStartLatitude,sStartLongitude,j);
        }
        return view;
    }
    public void getSpecialSendOrderInformation(){
        //获取开始地点名称
        locPlace=FixPosition.getLocPlace();
        //结束地点经纬度  double型
        dEndLongitude =PoiSearchActivity.getLongitude();
        dEndLatitude =PoiSearchActivity.getLatitude();
        //结束地点经纬度  String型
        sEndLongitude=dEndLongitude+"";
        sEndLatitude=dEndLatitude+"";
        //开始地点经纬度  String型
        dStartLongitude =FixPosition.getStartLongitude();
        dStartLatitude =FixPosition.getStartLatitude();
        //开始地点经纬度  double型
        sStartLongitude= dStartLongitude +"";
        sStartLatitude= dStartLatitude +"";
    }
    public int clickSpecialCarType(final RadioGroup rg_special, final View view){
        rg_special.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                rb_special= (RadioButton) view.findViewById(rg_special.getCheckedRadioButtonId());
                rb_id=rb_special.getId();
                if(sEndLongitude.equals("0.0")&&sEndLatitude.equals("0.0")){
                    Toast.makeText(getActivity(),"请输入结束地点",Toast.LENGTH_SHORT).show();
                }
                if(rb_id==R.id.rb1_special){
                    SendOrder.sendOrder(tv_money_special,newToken,locPlace,sStartLongitude,sStartLatitude,endAdress,sEndLongitude,sEndLatitude,"0");
                }else if(rb_id==R.id.rb2_special){
                    SendOrder.sendOrder(tv_money_special,newToken,locPlace,sStartLongitude,sStartLatitude,endAdress,sEndLongitude,sEndLatitude,"1");
                }else if(rb_id==R.id.rb3_special){
                    SendOrder.sendOrder(tv_money_special,newToken,locPlace,sStartLongitude,sStartLatitude,endAdress,sEndLongitude,sEndLatitude,"2");
                }
            }
        });
        return rb_id;
    }
    @Override
    public void onPause() {
        mapView.onPause();
        super.onPause();
    }

    @Override
    public void onResume() {
        mapView.onResume();
        //实现定位功能
        FixPosition.FixPosition(mapView,baiduMap,getContext(),geoCoder,tv_fragment_special_start);
        super.onResume();
    }

    @Override
    public void onDestroy() {
        //退出时销毁定位
        //关闭定位图层
        baiduMap.setMyLocationEnabled(false);
        mapView.onDestroy();
        mapView=null;
        super.onDestroy();
    }
}

